package org.dromara.common.alarm.message;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.cloud.alarm.dinger.constant.DingerConstant;
import com.cloud.alarm.dinger.core.entity.DingerRequest;
import com.cloud.alarm.dinger.support.CustomMessage;
import org.dromara.common.core.utils.SpringUtils;

import java.text.MessageFormat;
import java.util.List;

/**
 * 默认markdown消息格式
 *
 * @author shuai.zhou
 * @since 1.0
 */
public class MarkDownMessage implements CustomMessage {

    @Override
    public String message(DingerRequest request) {
        String applicationName = SpringUtils.getApplicationName();
        String content = request.getContent();
        String title = StrUtil.isNotBlank(request.getTitle()) ? request.getTitle() : "Dinger通知";
        String env = SpringUtil.getActiveProfile();
        List<String> phones = request.getPhones();
        // markdown在text内容里需要有@手机号
        StringBuilder text = new StringBuilder(title);
        if (phones != null && !phones.isEmpty()) {
            for (String phone : phones) {
                text.append(DingerConstant.DINGER_AT).append(phone);
            }
        }
        return MessageFormat.format(
            "【{0}】 {1}\n- 环境: 【{2}】.\n- 内容: {3}.\n- 时间: {4}",
            text, applicationName, env, content, DateUtil.now());
    }

}
